//
//  B_SelectView.m
//  testsdfsdfsdf
//
//  Created by 薛小刚 on 2018/8/11.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_SelectNavView.h"

@interface B_SelectNavView()

@property (nonatomic, assign) CGRect myFrame;

@property (nonatomic, strong) UIView * lineView;

@property (nonatomic, strong) NSMutableArray * buttonArray;

@end

@implementation B_SelectNavView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _myFrame = frame;
        _buttonArray = [NSMutableArray array];
    }
    return self;
}

- (void)setTitleArray:(NSArray *)titleArray {
    _titleArray = titleArray;
    CGFloat width = _myFrame.size.width / (titleArray.count * 1.0);
    for (int i = 0; i < _titleArray.count; i++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:_titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor color_text_white] forState:UIControlStateNormal];
        button.frame = CGRectMake(width * i, 0, width, _myFrame.size.height - 2);
        if (i == 0) {
            button.titleLabel.font = [UIFont boldSystemFontOfSize:18];
            self.lineView.frame = CGRectMake(0, _myFrame.size.height - 2, width, 2);
            [self addSubview:self.lineView];
            button.selected = YES;
        } else {
            button.titleLabel.font = [UIFont systemFontOfSize:18];
            button.selected = NO;
        }
        button.tag = i;
        [self.buttonArray addObject:button];
        [self addSubview:button];
        [button addTarget:self action:NSSelectorFromString(@"buttonAction:") forControlEvents:UIControlEventTouchUpInside];
    }
}

- (UIView *) lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor color_alert_background_color];
        _lineView.layer.masksToBounds = YES;
        _lineView.layer.cornerRadius = 1;
    }
    return _lineView;
}

- (void) buttonAction:(UIButton *) btn {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        return ;
    });
    for (UIButton * button in self.buttonArray) {
        if (button.isSelected) {
            button.selected = NO;
            button.titleLabel.font = [UIFont systemFontOfSize:18];
            break;
        }
    }
    btn.selected = YES;
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    if (self.tapActionWithIndex) {
        self.tapActionWithIndex(btn.tag);
    }
    [UIView animateWithDuration:0.2 animations:^{
        self.lineView.transform = CGAffineTransformMakeTranslation(btn.frame.origin.x, 0);
    }];
    
}


/**
 设置只是标杆的
 
 @param pointX x的位置,计算之前的
 */
- (void) setFrameOfLineView:(CGFloat) pointX {
    CGFloat width =  _myFrame.size.width / (self.buttonArray.count * 1.0);
    self.lineView.transform = CGAffineTransformMakeTranslation(pointX * width, 0);
//    _lineView.centerX = pointX * width  + width / 2.;
}


@end
